<?php
//
// jQuery File Tree PHP Connector
//
// Version 1.01
//
// Cory S.N. LaViska
// A Beautiful Site (http://abeautifulsite.net/)
// 24 March 2008
//
// History:
//
// 1.01 - updated to work with foreign characters in directory/file names (12 April 2008)
// 1.00 - released (24 March 2008)
//
// Output a list of files for jQuery File Tree
//

// Get passed parameter
$root = $_POST['dir'];

// Connecting to WP db
include_once('../wp-config.php');
include_once('../wp-load.php');
include_once('../wp-includes/wp-db.php');

// Show WP errors
$wpdb->show_errors();

// Instantiate array
$list = array();
$cat = "";	
		
// Get all

if(strpos($root,'/colleges/') !== false){
	// Inital variables
	$cat = '';
	$ary = explode("/", $root);
	$ary_size = count($ary);
	// First IF=Listing all colleges.... Second IF: Listing all disciplines in College...... Third IF: Listing all courses in discipline (in college)
	if (isset($ary[1]) && (strlen($ary[2]) < 1) && $ary_size == 3){
		$cat = "colleges";
		$list = $wpdb->get_results( "SELECT * FROM colleges" );
		// Make menu
		echo "<ul class=\"jqueryFileTree\" style=\"display: none;\">";
		// List all colleges
		foreach( $list as $item ) {
				echo "<li class=\"directory collapsed\"><a href=\"#\" rel=\"/" .$cat."/".$item->college_id ."/\">" . $item->college_id ." - ". $item->name . "</a></li>";
		}
		// End menu
		echo "</ul>";	
		
	} else if (isset($ary[1]) && (strlen($ary[2]) > 0) && $ary_size == 4){
		$col_id = $ary[2];
		$cat = "discipline";
		$list = $wpdb->get_results( "SELECT * FROM discipline WHERE college_id=".$col_id );
		// Make menu
		echo "<ul class=\"jqueryFileTree\" style=\"display: none;\">";
		// All all disciplines in college
		foreach( $list as $item ) {
				echo "<li class=\"directory collapsed\"><a href=\"#\" rel=\"/"."colleges"."/". $col_id ."/".$cat."/".$item->disc_id."/\">" . $item->disc_id ." - ". $item->name . "</a></li>";
		}
		// End menu
		echo "</ul>";	
	} else if (isset($ary[1]) && (strlen($ary[2]) > 0) && $ary_size == 6){
		$disc_id = $ary[4];
		$cat = "course";
		// Query database for listings
		$list = $wpdb->get_results( "SELECT * FROM courses WHERE disc_id=".$disc_id );
		// Make menu
		echo "<ul class=\"jqueryFileTree\" style=\"display: none;\">";
		// All courses in disciplines in college
		foreach( $list as $item ) {
				echo "<li class=\"course collapsed\"><a href=\"#\" rel=\"/"."colleges"."/". $ary[2] ."/"."discipline"."/".$item->disc_id."/".$cat."/".$item->course_id."/\">" . $item->course_id ." - ". $item->name . "</a></li>";
		}
		// End menu
		echo "</ul>";		
	}
	
} else if ($root == '/colleges/discipline/') {
	$list = $wpdb->get_results( "SELECT * FROM discipline" );
	$cat = "discipline";
}

//$files = scandir($root . $_POST['dir']);
//natcasesort($files);




?>